//*****************************************************************************
// 1. Års eksamen @ Datamatiker - EASJ
// Holmegaard Heldagsskole Administrations system 
// 
// Denne klasse er udarbejdet af: Marc og Kasper
//*****************************************************************************
package view;

import control.ControlUtil;
import javax.swing.table.DefaultTableModel;
import model.Fag;
import model.Trinmaal;

public class TrinmaalAdminPanel extends javax.swing.JPanel {

    private ControlUtil controlUtil;
    private Trinmaal[] tm;
    private boolean rediger, ny;
    private DefaultTableModel tabelModel;

    public TrinmaalAdminPanel(ControlUtil controlUtil) {
        this.controlUtil = controlUtil;
        ny = false;
        String col[] = {"Fag", "Årgang", "Trinmål"};
        String data[][] = null;
        tabelModel = new DefaultTableModel(data, col) {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        initComponents();
        nulstil();
        hentTrinmaal();
    }
    
     private void nulstil() {

        trinmaalLabel.setText("Tilføj Trinmål:");
        trinTekst.setEnabled(false);
        aargangCombo.setEnabled(false);
        redigerKnap.setEnabled(false);
        sletKnap.setEnabled(false);
        fagCombo.setEnabled(false);
        opretKnap.setEnabled(true);
        rediger = false;
    }

    private void hentTrinmaal() {


        tm = controlUtil.getTrinmaalHandler().hentTrinmaal();
        rydTabel();
        for (int i = 0; i < tm.length; i++) {

            tabelModel.addRow(new Object[]{tm[i].getFag(), tm[i].getKlassetrin(), tm[i].getTrinTekst()});

        }
        boolean before = fagCombo.isEnabled();
        fagCombo.removeAllItems();
        fagCombo.setEnabled(before);
        Fag[] f = controlUtil.getFagHandler().hentFag();
        fagCombo.addItem("Vælg fag");
        for (int i = 0; i < f.length; i++) {
            fagCombo.addItem(f[i]);

        }
    }

    private void rydTabel() {
        while (tabelModel.getRowCount() > 0) {
            tabelModel.removeRow(0);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabel = new javax.swing.JTable(tabelModel);
        jLabel1 = new javax.swing.JLabel();
        redigerKnap = new javax.swing.JButton();
        sletKnap = new javax.swing.JButton();
        trinmaalLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        fagCombo = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        aargangCombo = new javax.swing.JComboBox();
        trinTekst = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        opretKnap = new javax.swing.JButton();

        setLayout(null);

        tabel.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabel.getTableHeader().setReorderingAllowed(false);
        tabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tabelMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tabel);

        add(jScrollPane1);
        jScrollPane1.setBounds(20, 40, 860, 410);

        jLabel1.setText("Eksisterende trinmål:");
        add(jLabel1);
        jLabel1.setBounds(20, 20, 160, 14);

        redigerKnap.setText("Rediger trinmål");
        redigerKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redigerKnapActionPerformed(evt);
            }
        });
        add(redigerKnap);
        redigerKnap.setBounds(890, 490, 110, 40);

        sletKnap.setForeground(new java.awt.Color(255, 0, 0));
        sletKnap.setText("Slet trinmål");
        sletKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sletKnapActionPerformed(evt);
            }
        });
        add(sletKnap);
        sletKnap.setBounds(890, 360, 110, 40);

        trinmaalLabel.setText("Tilføj Trinmål:");
        add(trinmaalLabel);
        trinmaalLabel.setBounds(20, 470, 130, 14);

        jLabel4.setText("Fag");
        add(jLabel4);
        jLabel4.setBounds(20, 490, 34, 14);

        fagCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "qqq" }));
        fagCombo.setEnabled(false);
        fagCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fagComboActionPerformed(evt);
            }
        });
        add(fagCombo);
        fagCombo.setBounds(50, 490, 130, 20);

        jLabel5.setText("Årgang");
        add(jLabel5);
        jLabel5.setBounds(210, 490, 60, 14);

        aargangCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0. Klasse", "1. Klasse", "2. Klasse", "3. Klasse", "4. Klasse", "5. Klasse", "6. Klasse", "7. Klasse", "8. Klasse", "9. Klasse", "10. Klasse" }));
        aargangCombo.setEnabled(false);
        aargangCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aargangComboActionPerformed(evt);
            }
        });
        add(aargangCombo);
        aargangCombo.setBounds(260, 490, 90, 20);

        trinTekst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trinTekstActionPerformed(evt);
            }
        });
        trinTekst.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                trinTekstKeyReleased(evt);
            }
        });
        add(trinTekst);
        trinTekst.setBounds(420, 490, 460, 20);

        jLabel6.setText("Trinmål");
        add(jLabel6);
        jLabel6.setBounds(380, 490, 70, 14);

        opretKnap.setText("Opret trinmål");
        opretKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                opretKnapActionPerformed(evt);
            }
        });
        add(opretKnap);
        opretKnap.setBounds(890, 430, 110, 40);
    }// </editor-fold>//GEN-END:initComponents

    private void fagComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fagComboActionPerformed
        if (fagCombo.getSelectedIndex() != 0 && fagCombo.getItemCount() != 0) {


            aargangCombo.setEnabled(true);
        } else {
            aargangCombo.setEnabled(false);
        }
    }//GEN-LAST:event_fagComboActionPerformed

    private void aargangComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aargangComboActionPerformed
        trinTekst.setEnabled(true);
    }//GEN-LAST:event_aargangComboActionPerformed

    private void trinTekstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trinTekstActionPerformed
    }//GEN-LAST:event_trinTekstActionPerformed

    private void redigerKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redigerKnapActionPerformed
        if (rediger == true) {
            trinmaalLabel.setText("Tilføj Trinmål:");
            fagCombo.setEnabled(false);
            aargangCombo.setEnabled(false);
            trinTekst.setEnabled(false);
            opretKnap.setEnabled(true);
            redigerKnap.setText("Rediger trinmål");
            Trinmaal t = (Trinmaal) tm[tabel.getSelectedRow()];
            t.setFag((Fag) fagCombo.getSelectedItem());
            t.setKlassetrin(aargangCombo.getSelectedIndex() + "");
            t.setTrinTekst(trinTekst.getText());
            controlUtil.getTrinmaalHandler().updateTrinmaal(t);
            nulstil();

            hentTrinmaal();
        } else if (ny == true) {
            trinmaalLabel.setText("Tilføj Trinmål:");
            fagCombo.setEnabled(false);
            aargangCombo.setEnabled(false);
            trinTekst.setEnabled(false);
            opretKnap.setEnabled(true);
            redigerKnap.setText("Rediger trinmål");
            Trinmaal t = new Trinmaal(trinTekst.getText(), aargangCombo.getSelectedIndex() + "");
            t.setFag((Fag) fagCombo.getSelectedItem());
            controlUtil.getTrinmaalHandler().nytTrin(t);
            nulstil();
            hentTrinmaal();
            ny = false;

        } else {
            trinmaalLabel.setText("Rediger trinmål:");
            fagCombo.setEnabled(true);
            aargangCombo.setEnabled(true);
            trinTekst.setEnabled(true);
            redigerKnap.setText("Gem trinmål");
            opretKnap.setEnabled(false);
            rediger = true;
        }


    }//GEN-LAST:event_redigerKnapActionPerformed

    private void opretKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_opretKnapActionPerformed

        trinmaalLabel.setText("Opret trinmål:");
        fagCombo.setEnabled(true);
        aargangCombo.setEnabled(false);
        trinTekst.setEnabled(false);
        redigerKnap.setText("opret trinmål");
        opretKnap.setEnabled(false);
        rediger = false;
        ny = true;
        trinTekst.setText("");
        aargangCombo.setSelectedIndex(0);
        fagCombo.setSelectedIndex(0);
    }//GEN-LAST:event_opretKnapActionPerformed

    private void sletKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sletKnapActionPerformed
        Trinmaal t = (Trinmaal) tm[tabel.getSelectedRow()];
        controlUtil.getTrinmaalHandler().sletTrin(t);

        nulstil();
        hentTrinmaal();

    }//GEN-LAST:event_sletKnapActionPerformed

    private void trinTekstKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_trinTekstKeyReleased

        if (ny == true) {
            redigerKnap.setText("Opret trinmål");
        } else {
            redigerKnap.setText("Gem trinmål");
        }
        redigerKnap.setEnabled(true);
    }//GEN-LAST:event_trinTekstKeyReleased

    private void tabelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelMousePressed
        Trinmaal t = (Trinmaal) tm[tabel.getSelectedRow()];

        fagCombo.setSelectedItem(t.getFag());
        aargangCombo.setSelectedIndex(Integer.parseInt(t.getKlassetrin()));

        trinTekst.setText(t.getTrinTekst());

        opretKnap.setVisible(true);
        redigerKnap.setText("Rediger trinmål");
        rediger = false;
        ny = false;
        redigerKnap.setEnabled(true);
        sletKnap.setEnabled(true);
        fagCombo.setEnabled(false);
        aargangCombo.setEnabled(false);
        trinTekst.setEnabled(false);

    }//GEN-LAST:event_tabelMousePressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox aargangCombo;
    private javax.swing.JComboBox fagCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton opretKnap;
    private javax.swing.JButton redigerKnap;
    private javax.swing.JButton sletKnap;
    private javax.swing.JTable tabel;
    private javax.swing.JTextField trinTekst;
    private javax.swing.JLabel trinmaalLabel;
    // End of variables declaration//GEN-END:variables
}
